/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pscombine.extractor;

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import pscombine.constants.Constants;
import pscombine.database.DBManager;
import pscombine.ftp.FTP;

/**
 *
 * @author srkrishnan
 */
public class Category implements Extractor, Constants
{

    HashMap<Integer, Integer> langmap = null;
    HashMap<Integer, Integer> imageidmap = null;
    HashMap<Integer, Integer> idattributegroupmap = null;
    HashMap<Integer, Integer> attributemap = null;
    HashMap<Integer, Integer> featuremap = null;
    HashMap<Integer, Integer> featurevaluemap = null;
    String srcShop;
    String destShop;
    FTPClient srcftp = null;
    FTPClient destftp = null;
    static int insertedproducts = 0;
    static int MAX_PRODUCTS = 5;
    static int MAX_VALUE = 10;
    static int MIN_VALUE = 20;

    public String getDestShop()
    {
        return destShop;
    }

    public void setDestShop(String destShop)
    {
        this.destShop = destShop;
    }

    public String getSrcShop()
    {
        return srcShop;
    }

    public void setSrcShop(String srcShop)
    {
        this.srcShop = srcShop;
    }

//    public Category(String srcShop, String destShop) {
//        this.srcShop = srcShop;
//        this.destShop = destShop;
//    }
    public void getids(Connection conn)
    {
        try
        {
            // our SQL SELECT query. 
            // if you only need a few columns, specify them by name instead of using "*"
            String query = "SELECT * FROM " + DB_PREFIX + "category";

            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(query);

            // iterate through the java resultset
            while (rs.next())
            {
                int id = rs.getInt("id_category");

                // print the results
                System.out.format("%d\n", id);
            }
            st.close();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    public Connection connectDB(String shopname)
    {
        DBManager db = null;
        try
        {
            Configuration config = new PropertiesConfiguration("my.properties");
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://" + config.getString(shopname + ".dbhost") + ":" + config.getString(shopname + ".dbport") + "/";
            String userName = config.getString(shopname + ".dbuser");
            String password = config.getString(shopname + ".dbpassword");
            db = new DBManager(url, config.getString(shopname + ".database"), driver, userName, password);
            db.connectDB();
        } catch (ConfigurationException ex)
        {
            ex.printStackTrace();
        }
        return db.getConnection();
    }

    public void connectSrcFTP()
    {
        try
        {
            Configuration config = new PropertiesConfiguration("my.properties");
            srcftp = new FTPClient();
            srcftp.connect(config.getString(srcShop + ".hostname"));
            srcftp.login(config.getString(srcShop + ".username"), config.getString(srcShop + ".password"));
            srcftp.changeDirectory(config.getString(srcShop + ".basepath") + "img/p/");
            srcftp.setPassive(false);
            System.out.println("Connected to FTP... " + srcftp.getHost());
        } catch (IllegalStateException ex)
        {
            ex.printStackTrace();
        } catch (IOException ex)
        {
            ex.printStackTrace();
        } catch (FTPIllegalReplyException ex)
        {
            ex.printStackTrace();
        } catch (FTPException ex)
        {
            ex.printStackTrace();
        } catch (ConfigurationException ex)
        {
            ex.printStackTrace();
        }
    }

    public void connectDestFTP()
    {
        try
        {
            Configuration config = new PropertiesConfiguration("my.properties");
            destftp = new FTPClient();
            destftp.connect(config.getString(destShop + ".hostname"));
            destftp.login(config.getString(destShop + ".username"), config.getString(destShop + ".password"));
            destftp.changeDirectory(config.getString(destShop + ".basepath") + "img/p/");
            destftp.setPassive(false);
            System.out.println("Connected to FTP... " + destftp.getHost());
        } catch (IllegalStateException ex)
        {
            ex.printStackTrace();
        } catch (IOException ex)
        {
            ex.printStackTrace();
        } catch (FTPIllegalReplyException ex)
        {
            ex.printStackTrace();
        } catch (FTPException ex)
        {
            ex.printStackTrace();
        } catch (ConfigurationException ex)
        {
            ex.printStackTrace();
        }
    }

    public void copyRecords(Connection src, Connection dest, String srcShop, String destShop, HashMap<Integer, Integer> langmap, HashMap<Integer, Integer> imageidmap)
    {

        this.langmap = langmap;
        this.imageidmap = imageidmap;
        this.srcShop = srcShop;
        this.destShop = destShop;

        try
        {
            //create new category for site and pass it
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String now = dateFormat.format(date);

            String query = "insert into ps_category "
                    + "	(id_category, "
                    + "	id_parent, "
                    + "	level_depth, "
                    + "	nleft, "
                    + "	nright, "
                    + "	active, "
                    + "	date_add, "
                    + "	date_upd, "
                    + "	position"
                    + "	)"
                    + "	values"
                    + "	(0,"
                    + "1,"
                    + "1,"
                    + "1,"
                    + "1,"
                    + "1,"
                    + "\'" + now + "\',"
                    + "\'" + now + "\',"
                    + "0)";

            while (dest.isClosed())
            {
                dest = connectDB(destShop);
            }

            Statement st = dest.createStatement();

            st.executeUpdate(query);

            ResultSet rs = st.getGeneratedKeys();

            int autoIncValue = -1;

            if (rs.next())
            {
                autoIncValue = rs.getInt(1);
                /*
                 * You can get more generated keys if they are generated in your code
                 */
            }


            int parentid = autoIncValue;

            idattributegroupmap = new HashMap<Integer, Integer>();
            attributemap = new HashMap<Integer, Integer>();
            featuremap = new HashMap<Integer, Integer>();
            featurevaluemap = new HashMap<Integer, Integer>();


            Configuration config = new PropertiesConfiguration("my.properties");

//            connectSrcFTP();
//
//            connectDestFTP();



            //insert attribute group
            insertattributeGroup(dest, src);

            //insert attribute group lang
            insertAttributeGroupLang(dest, src);

            //insert attribute
            insertAttribute(dest, src);

            //insert attribute lang
            insertAttributeLang(dest, src);

            //insert feature
            insertFeature(dest, src);

            //insert feature lang
            insertFeatureLang(dest, src);


            //insert feature value
            insertFeatureValue(dest, src);

            //insert feature value lang
            insertFeatureValueLang(dest, src);



            copyCategoryRecords(src, dest, parentid, 1);
        } catch (Exception ex)
        {
            ex.printStackTrace();
        } finally
        {
            try
            {
//                System.out.println("Disconnecting ftp..." + srcShop);
//                //srcclient.logout();
//                if (srcftp != null && srcftp.isConnected()) {
//                    srcftp.disconnect(true);
//                }
//                System.out.println("Disconnecting ftp..." + destShop);
//                //destclient.logout();
//                if (destftp != null && destftp.isConnected()) {
//                    destftp.disconnect(true);
//                }
            } catch (IllegalStateException ex)
            {
                ex.printStackTrace();
            }

        }

    }

    private void copyCategoryRecords(Connection src, Connection dest, int parentid, int oldid)
    {

        try
        {
            //get all rows with "parentid" = rowid
            String query = "SELECT * FROM " + DB_PREFIX + "category where id_parent=" + oldid;

            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement st = src.createStatement();

            ResultSet rs = st.executeQuery(query);

            while (rs.next())
            {
                int id = rs.getInt("id_category");
                // print the results
                //System.out.format("%d\n", id);



                int newcatid = insertCategory(dest, rs, parentid);


                //insert category_group
                insertCategoryGroup(dest, src, newcatid, id);

                //insert category_lang
                insertCategoryLang(dest, src, newcatid, id);

                //insert category_product
                insertCategoryProduct(dest, src, newcatid, id);

                //continue recursion
                copyCategoryRecords(src, dest, newcatid, id);



            }
            st.close();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }

    }

    private int insertCategory(Connection dest, ResultSet rs, int parentid)
    {
        try
        {
            int level_depth = rs.getInt("level_depth");
            level_depth++;

            int oldcategoryid = rs.getInt("id_category");
            int nleft = rs.getInt("nleft");

            int nright = rs.getInt("nright");

            int active = rs.getInt("active");

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date_add = rs.getTimestamp("date_add");

            Date date_upd = rs.getTimestamp("date_upd");

            int position = rs.getInt("position");


            //insert record into destination database
            String catquery = "insert into ps_category "
                    + "	(id_category, "
                    + "	id_parent, "
                    + "	level_depth, "
                    + "	nleft, "
                    + "	nright, "
                    + "	active, "
                    + "	date_add, "
                    + "	date_upd, "
                    + "	position"
                    + "	)"
                    + "	values"
                    + "	(0,"
                    + parentid + ","
                    + level_depth + ","
                    + nleft + ","
                    + nright + ","
                    + active + ","
                    + "\'" + dateFormat.format(date_add) + "\',"
                    + "\'" + dateFormat.format(date_upd) + "\',"
                    + position + ")";


            while (dest.isClosed())
            {
                dest = connectDB(destShop);
            }
            Statement stcat = dest.createStatement();

            stcat.executeUpdate(catquery);

            ResultSet rscat = stcat.getGeneratedKeys();

            int autoIncValue = -1;

            if (rscat.next())
            {
                autoIncValue = rscat.getInt(1);
                /*
                 * You can get more generated keys if they are generated in your code
                 */
            }
            System.out.println("Added Category " + autoIncValue);

            int newcategoryid = autoIncValue;

            Configuration config = new PropertiesConfiguration("my.properties");
            String srcimg = "C:\\wamp\\www\\" + config.getString(srcShop + ".basepath") + "img/c/" + oldcategoryid;
            String destimg = "C:\\wamp\\www\\" + config.getString(destShop + ".basepath") + "img/c/" + newcategoryid;
            System.out.println("Copying " + srcimg + ".jpg to " + destimg + ".jpg");
            copyimage(srcimg + ".jpg", destimg + ".jpg");

            //copy extra images

            ArrayList<String> imagetypes = new ArrayList<String>();
            imagetypes.add("category");
            imagetypes.add("large");
            imagetypes.add("medium");
            imagetypes.add("small");



            for (String imgtype : imagetypes)
            {
                System.out.println("Copying " + srcimg + "-" + imgtype + ".jpg to " + destimg + "-" + imgtype + ".jpg");
                copyimage(srcimg + "-" + imgtype + ".jpg", destimg + "-" + imgtype + ".jpg");
            }
            //end copy extra images

            return autoIncValue;
        } catch (ConfigurationException ex)
        {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        return -1;
    }

    private void insertCategoryGroup(Connection dest, Connection src, int newcatid, int oldid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "category_group where id_category=" + oldid;

            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);

            int id_group = -1;
            while (rs.next())
            {
                id_group = rs.getInt("id_group");
                break;
            }

            query = "insert into " + DB_PREFIX + "category_group"
                    + " (id_category,id_group)"
                    + " values"
                    + "("
                    + newcatid + ","
                    + id_group + ")";
            while (dest.isClosed())
            {
                dest = connectDB(destShop);
            }
            stcat = dest.createStatement();
            stcat.executeUpdate(query);
            System.out.println("Added category group " + newcatid + " = " + id_group);

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }



    }

    private void insertCategoryLang(Connection dest, Connection src, int newcatid, int oldid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "category_lang where id_category=" + oldid;

            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_lang = 0;
                if (langmap.get(rs.getInt("id_lang")) != null)
                {
                    id_lang = langmap.get(rs.getInt("id_lang"));
                } else
                {
                    continue;
                }
                String name = rs.getString("name");
                String description = rs.getString("description");
                String link_rewrite = rs.getString("link_rewrite");
                String meta_title = rs.getString("meta_title");
                String meta_keywords = rs.getString("meta_keywords");
                String meta_description = rs.getString("meta_description");

                query = "insert into " + DB_PREFIX + "category_lang"
                        + " (id_category,"
                        + "id_lang, "
                        + "name,"
                        + " description,"
                        + " link_rewrite,"
                        + " meta_title,"
                        + " meta_keywords,"
                        + " meta_description)"
                        + " values"
                        + "(?,?,?,?,?,?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, newcatid);
                psmt.setInt(2, id_lang);
                psmt.setString(3, name);
                psmt.setString(4, description);
                psmt.setString(5, link_rewrite);
                psmt.setString(6, meta_title);
                psmt.setString(7, meta_keywords);
                psmt.setString(8, meta_description);
                psmt.executeUpdate();
                System.out.println("Added Category Lang " + newcatid + " =" + name);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }


    }

    private void insertCategoryProduct(Connection dest, Connection src, int newcatid, int oldid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "category_product where id_category=" + oldid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }


            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                Random random = new Random();
                int myRandomNumber = random.nextInt(MAX_VALUE - MIN_VALUE) + MIN_VALUE;

                if(myRandomNumber%15 != 0)
                {
                    continue;
                }
                
                if (insertedproducts > MAX_PRODUCTS)
                {
                    break;
                }

                int productid = rs.getInt("id_product");
                int newproductid = insertProduct(dest, src, productid);

                //insert product_lang
                insertProductLang(dest, src, productid, newproductid);

                //insert image
                insertImage(dest, src, productid, newproductid);

                //insert specific_price
                insertSpecificPrice(dest, src, productid, newproductid);




                //insert attributes - product_attribute
                insertProductAttribute(dest, src, productid, newproductid);


                //insert feature product
                insertFeatureProduct(dest, src, productid, newproductid);

                insertedproducts++;

                int position = rs.getInt("position");


                query = "insert into " + DB_PREFIX + "category_product"
                        + " (id_category,"
                        + "id_product, "
                        + "position)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, newcatid);
                psmt.setInt(2, newproductid);
                psmt.setInt(3, position);

                psmt.executeUpdate();
                System.out.println("Added Category Product " + newcatid + " =" + newproductid);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }

    }

    private int insertProduct(Connection dest, Connection src, int productid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "product where id_product=" + productid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_supplier = rs.getInt("id_supplier");
                int id_manufacturer = rs.getInt("id_manufacturer");
                int id_tax_rules_group = rs.getInt("id_tax_rules_group");
                int id_category_default = rs.getInt("id_category_default");
                int id_color_default = rs.getInt("id_color_default");
                int on_sale = rs.getInt("on_sale");
                int online_only = rs.getInt("online_only");
                String ean13 = rs.getString("ean13");
                String upc = rs.getString("upc");
                Double ecotax = rs.getDouble("ecotax");
                int quantity = rs.getInt("quantity");
                int minimal_quantity = rs.getInt("minimal_quantity");
                Double price = rs.getDouble("price");
                Double wholesale_price = rs.getDouble("wholesale_price");
                String unity = rs.getString("unity");
                Double unit_price_ratio = rs.getDouble("unit_price_ratio");
                Double additional_shipping_cost = rs.getDouble("additional_shipping_cost");
                String reference = rs.getString("reference");
                String supplier_reference = rs.getString("supplier_reference");
                String location = rs.getString("location");
                Double width = rs.getDouble("width");
                Double height = rs.getDouble("height");
                Double depth = rs.getDouble("depth");
                Double weight = rs.getDouble("weight");
                int out_of_stock = rs.getInt("out_of_stock");
                int quantity_discount = rs.getInt("quantity_discount");
                int customizable = rs.getInt("customizable");
                int uploadable_files = rs.getInt("uploadable_files");
                int text_fields = rs.getInt("text_fields");
                int active = rs.getInt("active");
                int available_for_order = rs.getInt("available_for_order");
                String condition = rs.getString("condition");
                int show_price = rs.getInt("show_price");
                int indexed = rs.getInt("indexed");
                int cache_is_pack = rs.getInt("cache_is_pack");
                int cache_has_attachments = rs.getInt("cache_has_attachments");
                int cache_default_attribute = rs.getInt("cache_default_attribute");
                Timestamp date_add = rs.getTimestamp("date_add");
                Timestamp date_upd = rs.getTimestamp("date_upd");




                query = "insert into " + DB_PREFIX + "product"
                        + " (id_product,id_supplier, id_manufacturer, id_tax_rules_group, id_category_default, id_color_default, on_sale, online_only, ean13, upc, ecotax, quantity, minimal_quantity, price, wholesale_price, unity, unit_price_ratio, additional_shipping_cost, reference, supplier_reference, location, width, height, depth, weight, out_of_stock, quantity_discount, 	customizable, 	uploadable_files, text_fields, active, available_for_order, `condition`, show_price, indexed, cache_is_pack, cache_has_attachments, 	cache_default_attribute, date_add, date_upd)"
                        + " values"
                        + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.setInt(2, id_supplier);
                psmt.setInt(3, id_manufacturer);
                psmt.setInt(4, id_tax_rules_group);
                psmt.setInt(5, id_category_default);
                psmt.setInt(6, id_color_default);
                psmt.setInt(7, on_sale);
                psmt.setInt(8, online_only);
                psmt.setString(9, ean13);
                psmt.setString(10, upc);
                psmt.setDouble(11, ecotax);
                psmt.setInt(12, quantity);
                psmt.setInt(13, minimal_quantity);
                psmt.setDouble(14, price);
                psmt.setDouble(15, wholesale_price);
                psmt.setString(16, unity);
                psmt.setDouble(17, unit_price_ratio);
                psmt.setDouble(18, additional_shipping_cost);
                psmt.setString(19, reference);
                psmt.setString(20, supplier_reference);
                psmt.setString(21, location);
                psmt.setDouble(22, width);
                psmt.setDouble(23, height);
                psmt.setDouble(24, depth);
                psmt.setDouble(25, weight);
                psmt.setInt(26, out_of_stock);
                psmt.setInt(27, quantity_discount);
                psmt.setInt(28, customizable);
                psmt.setInt(29, uploadable_files);
                psmt.setInt(30, text_fields);
                psmt.setInt(31, active);
                psmt.setInt(32, available_for_order);
                psmt.setString(33, condition);
                psmt.setInt(34, show_price);
                psmt.setInt(35, indexed);
                psmt.setInt(36, cache_is_pack);
                psmt.setInt(37, cache_has_attachments);
                psmt.setInt(38, cache_default_attribute);
                psmt.setTimestamp(39, date_add);
                psmt.setTimestamp(40, date_upd);


                ////System.out.println(psmt);

                psmt.executeUpdate();


                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int newproductid = autoIncValue;

                System.out.println("added new product " + newproductid);
                return newproductid;
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        return 0;
    }

    private void insertProductLang(Connection dest, Connection src, int oldproductid, int newproductid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "product_lang where id_product=" + oldproductid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_lang = 0;
                if (langmap.get(rs.getInt("id_lang")) != null)
                {
                    id_lang = langmap.get(rs.getInt("id_lang"));
                } else
                {
                    continue;
                }

                String description = rs.getString("description");

                String description_short = rs.getString("description_short");

                String link_rewrite = rs.getString("link_rewrite");
                String meta_description = rs.getString("meta_description");
                String meta_keywords = rs.getString("meta_keywords");
                String meta_title = rs.getString("meta_title");
                String name = rs.getString("name");
                String available_now = rs.getString("available_now");
                String available_later = rs.getString("available_later");




                query = "insert into " + DB_PREFIX + "product_lang"
                        + " (id_product,id_lang,description,description_short,link_rewrite,meta_description,meta_keywords,meta_title,`name`,available_now,available_later)"
                        + " values"
                        + "(?,?,?,?,?,?,?,?,?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, newproductid);
                psmt.setInt(2, id_lang);
                psmt.setString(3, description);
                psmt.setString(4, description_short);
                psmt.setString(5, link_rewrite);
                psmt.setString(6, meta_description);
                psmt.setString(7, meta_keywords);
                psmt.setString(8, meta_title);
                psmt.setString(9, name);
                psmt.setString(10, available_now);
                psmt.setString(11, available_later);


                psmt.executeUpdate();
                System.out.println("Added product lang " + newproductid + " =" + name);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }

    }

    private void insertImage(Connection dest, Connection src, int productid, int newproductid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "image where id_product=" + productid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int oldimageid = rs.getInt("id_image");
                int position = rs.getInt("position");
                int cover = rs.getInt("cover");



                query = "insert into " + DB_PREFIX + "image"
                        + " (id_image,id_product,position,cover)"
                        + " values"
                        + "(?,?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.setInt(2, newproductid);
                psmt.setInt(3, position);
                psmt.setInt(4, cover);

                ////System.out.println(psmt);

                psmt.executeUpdate();


                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int newimageid = autoIncValue;

                FTP.transferfile(srcShop, destShop, srcftp, destftp, oldimageid, productid, newimageid, newproductid);

                Configuration config = new PropertiesConfiguration("my.properties");
                String srcimg = "C:\\wamp\\www\\" + config.getString(srcShop + ".basepath") + "img/p/" + productid + "-" + oldimageid;
                String destimg = "C:\\wamp\\www\\" + config.getString(destShop + ".basepath") + "img/p/" + newproductid + "-" + newimageid;
                System.out.println("Copying " + srcimg + ".jpg to " + destimg + ".jpg");
                copyimage(srcimg + ".jpg", destimg + ".jpg");

                //copy extra images

                ArrayList<String> imagetypes = new ArrayList<String>();
                imagetypes.add("flash");
                imagetypes.add("home");
                imagetypes.add("large");
                imagetypes.add("medium");
                imagetypes.add("small");
                imagetypes.add("thickbox");

                for (String imgtype : imagetypes)
                {
                    System.out.println("Copying " + srcimg + "-" + imgtype + ".jpg to " + destimg + "-" + imgtype + ".jpg");
                    copyimage(srcimg + "-" + imgtype + ".jpg", destimg + "-" + imgtype + ".jpg");
                }
                //end copy extra images



                insertImageLang(dest, src, oldimageid, newimageid);

                imageidmap.put(oldimageid, newimageid);

                System.out.println("Added image " + newimageid);
            }

        } catch (ConfigurationException ex)
        {
            ex.printStackTrace();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }

    }

    private void copyimage(String srcimg, String destimg)
    {
        InputStream in = null;
        try
        {
            if (new File(srcimg).exists() == false)
            {
                System.out.println(srcimg + " Image not found!!");
                return;
            }
            in = new FileInputStream(srcimg);

            OutputStream out = new FileOutputStream(destimg);

            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = in.read(buf)) > 0)
            {
                out.write(buf, 0, length);
            }
        } catch (IOException ex)
        {
            ex.printStackTrace();
        } finally
        {
            try
            {
                if (in != null)
                {
                    in.close();
                }
            } catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }

    private void copyimage_temp(String src, String dest)
    {
        FileReader in = null;
        {
            FileWriter out = null;
            try
            {
                File inputFile = new File(src);
                File outputFile = new File(dest);
                if (inputFile.exists() == false)
                {
                    System.out.println(inputFile.getName() + " Image does not exists!!");
                    return;
                }
                in = new FileReader(inputFile);
                out = new FileWriter(outputFile);
                int c;
                while ((c = in.read()) != -1)
                {
                    out.write(c);
                }
                in.close();
                out.close();
            } catch (IOException ex)
            {
                ex.printStackTrace();
            } finally
            {
                try
                {
                    if (in != null)
                    {
                        in.close();
                    }
                } catch (IOException ex)
                {
                    ex.printStackTrace();
                }
                try
                {
                    if (out != null)
                    {
                        out.close();
                    }
                } catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }

    private void insertImageLang(Connection dest, Connection src, int oldimageid, int newimageid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "image_lang where id_image=" + oldimageid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_lang = 0;
                if (langmap.get(rs.getInt("id_lang")) != null)
                {
                    id_lang = langmap.get(rs.getInt("id_lang"));
                } else
                {
                    continue;
                }

                String legend = rs.getString("legend");


                query = "insert into " + DB_PREFIX + "image_lang"
                        + " (id_image,id_lang,legend)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, newimageid);
                psmt.setInt(2, id_lang);
                psmt.setString(3, legend);

                psmt.executeUpdate();
                System.out.println("Added image lang " + legend);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertSpecificPrice(Connection dest, Connection src, int productid, int newproductid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "specific_price where id_product=" + productid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_specific_price = rs.getInt("id_specific_price");
                int id_product = rs.getInt("id_product");
                int id_shop = rs.getInt("id_shop");
                int id_currency = rs.getInt("id_currency");
                int id_country = rs.getInt("id_country");
                int id_group = rs.getInt("id_product");
                Double price = rs.getDouble("price");
                int from_quantity = rs.getInt("from_quantity");
                Double reduction = rs.getDouble("reduction");
                String reduction_type = rs.getString("reduction_type");


                String from = new String(rs.getBytes("from"));
                String to = new String(rs.getBytes("to"));


                query = "insert into " + DB_PREFIX + "specific_price"
                        + " (id_specific_price,id_product,id_shop,id_currency,id_country,id_group,price,from_quantity,reduction,reduction_type,`from`,`to`)"
                        + " values"
                        + "(?,?,?,?,?,?,?,?,?,?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.setInt(2, newproductid);
                psmt.setInt(3, id_shop);
                psmt.setInt(4, id_currency);
                psmt.setInt(5, id_country);
                psmt.setInt(6, id_group);
                psmt.setDouble(7, price);
                psmt.setInt(8, from_quantity);
                psmt.setDouble(9, reduction);
                psmt.setString(10, reduction_type);
//                psmt.setTimestamp(11, from);
//                psmt.setTimestamp(12, to);

                psmt.setString(11, from);
                psmt.setString(12, to);


                ////System.out.println(psmt);

                psmt.executeUpdate();


                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int newidspecificprice = autoIncValue;

                System.out.println("Added specific price " + newidspecificprice);


            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }

    }

    private void insertProductAttribute(Connection dest, Connection src, int productid, int newproductid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "product_attribute where id_product=" + productid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_product_attribute = rs.getInt("id_product_attribute");
                int id_product = rs.getInt("id_product");
                String reference = rs.getString("reference");
                String supplier_reference = rs.getString("supplier_reference");
                String location = rs.getString("location");
                String ean13 = rs.getString("ean13");
                String upc = rs.getString("upc");
                Double wholesale_price = rs.getDouble("wholesale_price");
                Double price = rs.getDouble("price");
                Double ecotax = rs.getDouble("ecotax");
                int quantity = rs.getInt("quantity");
                Double weight = rs.getDouble("weight");
                Double unit_price_impact = rs.getDouble("unit_price_impact");
                int default_on = rs.getInt("default_on");
                int minimal_quantity = rs.getInt("minimal_quantity");


//id_product_attribute,id_product,,,,,,,,,,,,, 

                query = "insert into " + DB_PREFIX + "product_attribute"
                        + " (id_product_attribute,id_product,reference,supplier_reference,location,ean13,upc,wholesale_price,price,ecotax,quantity,weight,unit_price_impact,default_on,minimal_quantity )"
                        + " values"
                        + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.setInt(2, newproductid);
                psmt.setString(3, reference);
                psmt.setString(4, supplier_reference);
                psmt.setString(5, location);
                psmt.setString(6, ean13);
                psmt.setString(7, upc);
                psmt.setDouble(8, wholesale_price);
                psmt.setDouble(9, price);
                psmt.setDouble(10, ecotax);
                psmt.setInt(11, quantity);
                psmt.setDouble(12, weight);
                psmt.setDouble(13, unit_price_impact);
                psmt.setInt(14, default_on);
                psmt.setInt(15, minimal_quantity);

                ////System.out.println(psmt);


                psmt.executeUpdate();


                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int newidproductattribute = autoIncValue;


                //insert product attribute combination
                insertProductAttributeCombination(dest, src, id_product_attribute, newidproductattribute);

                //insert attribute image
                insertAttributeImage(dest, src, id_product_attribute, newidproductattribute);

                System.out.println("Added product attribute " + newidproductattribute);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertattributeGroup(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "attribute_group";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_attribute_group = rs.getInt("id_attribute_group");
                int is_color_group = rs.getInt("is_color_group");



//id_product_attribute,id_product,,,,,,,,,,,,, 

                query = "insert into " + DB_PREFIX + "attribute_group"
                        + " (id_attribute_group,is_color_group)"
                        + " values"
                        + "(?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.setInt(2, is_color_group);


                ////System.out.println(psmt);

                psmt.executeUpdate();


                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int new_id_attribute_group = autoIncValue;

                idattributegroupmap.put(id_attribute_group, new_id_attribute_group);

                System.out.println("Added attribute group " + new_id_attribute_group);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertAttributeGroupLang(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "attribute_group_lang";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_attribute_group = 0;
                if (idattributegroupmap.get(rs.getInt("id_attribute_group")) != null)
                {
                    id_attribute_group = idattributegroupmap.get(rs.getInt("id_attribute_group"));
                } else
                {
                    continue;
                }
                int id_lang = 0;
                if (langmap.get(rs.getInt("id_lang")) != null)
                {
                    id_lang = langmap.get(rs.getInt("id_lang"));
                } else
                {
                    continue;
                }

                String name = rs.getString("name");
                String public_name = rs.getString("public_name");


//id_attribute_group,id_lang,`name`,public_name

                query = "insert into " + DB_PREFIX + "attribute_group_lang"
                        + " (id_attribute_group,id_lang,`name`,public_name)"
                        + " values"
                        + "(?,?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }

                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, id_attribute_group);
                psmt.setInt(2, id_lang);
                psmt.setString(3, name);
                psmt.setString(4, public_name);

                ////System.out.println(psmt);

                psmt.executeUpdate();

                System.out.println("Added attribute group lang " + name);



            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertAttribute(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "attribute";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_attribute_old = rs.getInt("id_attribute");
                int id_attribute_group = 0;
                if (idattributegroupmap.get(rs.getInt("id_attribute_group")) != null)
                {
                    id_attribute_group = idattributegroupmap.get(rs.getInt("id_attribute_group"));
                } else
                {
                    continue;
                }
                String color = rs.getString("color");

//id_attribute,id_attribute_group,color

                query = "insert into " + DB_PREFIX + "attribute"
                        + " (id_attribute,id_attribute_group,color)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.setInt(2, id_attribute_group);
                psmt.setString(3, color);
                ////System.out.println(psmt);

                psmt.executeUpdate();


                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int id_attribute_new = autoIncValue;

                attributemap.put(id_attribute_old, id_attribute_new);

                System.out.println("Added attribute " + id_attribute_new);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertAttributeLang(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "attribute_lang";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_attribute = 0;
                if (attributemap.get(rs.getInt("id_attribute")) != null)
                {
                    id_attribute = attributemap.get(rs.getInt("id_attribute"));
                } else
                {
                    continue;
                }
                int id_lang = 0;
                if (langmap.get(rs.getInt("id_lang")) != null)
                {
                    id_lang = langmap.get(rs.getInt("id_lang"));
                } else
                {
                    continue;
                }
                String name = rs.getString("name");

//id_attribute,id_lang,name

                query = "insert into " + DB_PREFIX + "attribute_lang"
                        + " (id_attribute,id_lang,name)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, id_attribute);
                psmt.setInt(2, id_lang);
                psmt.setString(3, name);
                ////System.out.println(psmt);
                psmt.executeUpdate();

                System.out.println("Added attribute lang " + name);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertProductAttributeCombination(Connection dest, Connection src, int id_product_attribute, int newidproductattribute)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "product_attribute_combination where id_product_attribute=" + id_product_attribute;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {

                int id_attribute = 0;
                if (attributemap.get(rs.getInt("id_attribute")) != null)
                {
                    id_attribute = attributemap.get(rs.getInt("id_attribute"));
                } else
                {
                    continue;
                }




                query = "insert into " + DB_PREFIX + "product_attribute_combination"
                        + " (id_attribute,id_product_attribute)"
                        + " values"
                        + "(?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, id_attribute);
                psmt.setInt(2, newidproductattribute);
                ////System.out.println(psmt);

                psmt.executeUpdate();

                System.out.println("Added product attribute combination " + newidproductattribute);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertAttributeImage(Connection dest, Connection src, int id_product_attribute, int newidproductattribute)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "product_attribute_image where id_product_attribute=" + id_product_attribute;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {


                int id_image = 0;
                if (imageidmap.get(rs.getInt("id_image")) != null)
                {
                    id_image = imageidmap.get(rs.getInt("id_image"));
                }




                query = "insert into " + DB_PREFIX + "product_attribute_image"
                        + " (id_product_attribute,id_image)"
                        + " values"
                        + "(?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, newidproductattribute);
                psmt.setInt(2, id_image);

                ////System.out.println(psmt);
                psmt.executeUpdate();
                System.out.println("Added attribute image " + newidproductattribute);

            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertFeatureProduct(Connection dest, Connection src, int productid, int newproductid)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "feature_product where id_product=" + productid;
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {


                int id_feature = 0;
                if (featuremap.get(rs.getInt("id_feature")) != null)
                {
                    id_feature = featuremap.get(rs.getInt("id_feature"));
                } else
                {
                    continue;
                }
                int id_product = rs.getInt("id_product");
                int id_feature_value = 0;
                if (featurevaluemap.get(rs.getInt("id_feature_value")) != null)
                {
                    id_feature_value = featurevaluemap.get(rs.getInt("id_feature_value"));
                } else
                {
                    continue;
                }

//id_feature,id_product,id_feature_value


                query = "insert into " + DB_PREFIX + "feature_product"
                        + " (id_feature,id_product,id_feature_value)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, id_feature);
                psmt.setInt(2, newproductid);
                psmt.setInt(3, id_feature_value);
                ////System.out.println(psmt);
                psmt.executeUpdate();
                System.out.println("Added feature product " + id_feature);

            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertFeature(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "feature";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {


                int id_feature_old = rs.getInt("id_feature");

                query = "insert into " + DB_PREFIX + "feature"
                        + " (id_feature)"
                        + " values"
                        + "(?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.executeUpdate();
                ////System.out.println(psmt);
                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int id_feature_new = autoIncValue;

                featuremap.put(id_feature_old, id_feature_new);

                System.out.println("Added feature " + id_feature_new);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertFeatureLang(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "feature_lang";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {


                int id_feature = 0;
                if (featuremap.get(rs.getInt("id_feature")) != null)
                {
                    id_feature = featuremap.get(rs.getInt("id_feature"));
                } else
                {
                    continue;
                }
                int id_lang = 0;
                if (langmap.get(rs.getInt("id_lang")) != null)
                {
                    id_lang = langmap.get(rs.getInt("id_lang"));
                } else
                {
                    continue;
                }
                String name = rs.getString("name");

                query = "insert into " + DB_PREFIX + "feature_lang"
                        + " (id_feature,id_lang,name)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, id_feature);
                psmt.setInt(2, id_lang);
                psmt.setString(3, name);
                ////System.out.println(psmt);
                psmt.executeUpdate();

                ResultSet rsprod = psmt.getGeneratedKeys();

                System.out.println("Added feature lang " + name);


            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertFeatureValue(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "feature_value";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {


                int id_feature_value_old = rs.getInt("id_feature_value");
                int id_feature = 0;
                if (featuremap.get(rs.getInt("id_feature")) != null)
                {
                    id_feature = featuremap.get(rs.getInt("id_feature"));
                } else
                {
                    continue;
                }
                int custom = rs.getInt("custom");


                query = "insert into " + DB_PREFIX + "feature_value"
                        + " (id_feature_value,id_feature,custom)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, 0);
                psmt.setInt(2, id_feature);
                psmt.setInt(3, custom);
                ////System.out.println(psmt);
                psmt.executeUpdate();

                ResultSet rsprod = psmt.getGeneratedKeys();

                int autoIncValue = -1;

                if (rsprod.next())
                {
                    autoIncValue = rsprod.getInt(1);
                    /*
                     * You can get more generated keys if they are generated in
                     * your code
                     */
                }

                int id_feature_value_new = autoIncValue;

                featurevaluemap.put(id_feature_value_old, id_feature_value_new);

                System.out.println("Added feature value " + id_feature_value_new);
            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void insertFeatureValueLang(Connection dest, Connection src)
    {
        try
        {
            String query = "SELECT * FROM " + DB_PREFIX + "feature_value_lang";
            while (src.isClosed())
            {
                src = connectDB(srcShop);
            }
            Statement stcat = src.createStatement();

            ResultSet rs = stcat.executeQuery(query);


            while (rs.next())
            {


                int id_feature_value = 0;
                if (featurevaluemap.get(rs.getInt("id_feature_value")) != null)
                {
                    id_feature_value = featurevaluemap.get(rs.getInt("id_feature_value"));
                } else
                {
                    continue;
                }
                int id_lang = 0;
                if (langmap.get(rs.getInt("id_lang")) != null)
                {
                    id_lang = langmap.get(rs.getInt("id_lang"));
                } else
                {
                    continue;
                }
                String value = rs.getString("value");

                query = "insert into " + DB_PREFIX + "feature_value_lang"
                        + " (id_feature_value,id_lang,value)"
                        + " values"
                        + "(?,?,?)";
                while (dest.isClosed())
                {
                    dest = connectDB(destShop);
                }
                PreparedStatement psmt = dest.prepareStatement(query);
                psmt.setInt(1, id_feature_value);
                psmt.setInt(2, id_lang);
                psmt.setString(3, value);
                ////System.out.println(psmt);
                psmt.executeUpdate();

                ResultSet rsprod = psmt.getGeneratedKeys();

                System.out.println("Added feature value lang " + value);


            }

        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }
}
